﻿using System.Drawing;
using System.Windows.Forms;

namespace SimpleTerminal.Render
{
    // class MyMenuRender : ToolStripProfessionalRenderer
    class ToolStripMenuRender : ToolStripSystemRenderer
    {
        public static void FillRoundRect(Graphics currentGraphicObject, Color fillColor, int nLeft, int nTop, int nRight, int nBottom, int round)
        {
            Point Polygon1 = new Point(nLeft + round, nTop);
            Point Polygon2 = new Point(nRight - round + 1, nTop);

            Point Polygon3 = new Point(nLeft, nTop + round);
            Point Polygon4 = new Point(nRight + 1, nTop + round);

            Point Polygon5 = new Point(nLeft, nBottom - round);
            Point Polygon6 = new Point(nRight + 1, nBottom - round);

            Point Polygon7 = new Point(nLeft + round, nBottom + 1);
            Point Polygon8 = new Point(nRight - round, nBottom + 1);

            using (SolidBrush solidBrush = new SolidBrush(fillColor))
            {
                currentGraphicObject.FillPolygon(solidBrush, new Point[]{   Polygon1,
                      Polygon3,
                      Polygon5,
                      Polygon7,
                      Polygon8,
                      Polygon6,
                      Polygon4,
                      Polygon2});
            }

        }
        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            //base.OnRenderButtonBackground(e);
            if (e.Item.Selected)
            {
                using (SolidBrush solidBrush = new SolidBrush(Color.FromArgb(0xef, 0xef, 0xef)))
                {
                    FillRoundRect(e.Graphics,Color.FromArgb(0xdf, 0xdf, 0xdf),0,0,e.Item.Width,e.Item.Height,3);
                    //e.Graphics.FillRectangle(solidBrush, 0, 0, e.Item.Width, e.Item.Height);
                }
            }
        }
        protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
        {
            //base.OnRenderItemBackground(e);
            if (e.Item.Enabled && e.Item.Selected)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Red),
                    new Rectangle(0, 0, e.Item.Width, e.Item.Height));
            }
            else
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Red),
                    new Rectangle(0, 0, e.Item.Width, e.Item.Height));
            }
        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            //if (e.Item.Enabled)
            //    base.OnRenderMenuItemBackground(e);
            if (e.Item.Enabled && e.Item.Selected)
            {
                e.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
                    new Rectangle(0, 0, e.Item.Width, e.Item.Height));
            }
            else
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White),
                    new Rectangle(0, 0, e.Item.Width, e.Item.Height));
            }
        }
        /// <summary>
        /// 渲染菜单项的分隔线
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
        {

            e.Graphics.DrawLine(new Pen(Color.FromArgb(200, 200, 200), 1), e.Item.Width / 2,
                5, e.Item.Width / 2, e.Item.Height - 5);
        }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            if (e.Item.Selected)
            {
                e.TextColor = Color.Black;
            }
            base.OnRenderItemText(e);
        }

        //  protected override 

        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            // base.OnRenderToolStripBorder(e);
            // e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0xcc, 0xcc, 0xcc), 1),e.AffectedBounds);
        }

        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            //  base.OnRenderToolStripBackground(e);
        }
    }
}
